package cn.kgc.vue.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.vue.commons.KgcLogs;
import cn.kgc.vue.commons.KgcOperation;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/12/14
 * @description
 */
@Api(tags = "学生模块")
@RestController
@RequestMapping("students")
@Slf4j
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation("学生信息列表")
    @PostMapping("list")
    public ResponseResult getStudentList(@RequestBody QueryCondition<Student> queryCondition) {

        return studentService.getStudentList(queryCondition);
    }


    @KgcLogs(model = "学生模块",operation = KgcOperation.UPDATE)
    @ApiOperation("添加/编辑学生信息")
    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdateStudentInfo(@RequestBody Student student) {
        // 1. 添加   判定学号是否存在   修改无需判定
        if (ObjectUtils.isNull(student.getId())) {
            LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
            lambda.eq(Student::getStudentNo, student.getStudentNo());
            Student stu = studentService.getOne(lambda);
            if (ObjectUtils.isNotNull(stu)) {
                return ResponseResult.fail().message("学号已存在");
            }
        }

        // 2.添加或修改学生信息
        studentService.saveOrUpdate(student);
        return ResponseResult.success();
    }

    @ApiOperation("根据id获取学生信息")
    @GetMapping("getById/{id}")
    public ResponseResult getStudentById(@PathVariable Integer id) {

        Student student = studentService.getById(id);
        return ResponseResult.success().data(student);
    }

    @KgcLogs(model = "学生模块",operation = KgcOperation.DELETE)
    @ApiOperation("根据id删除学生信息")
    @DeleteMapping("deleteById/{id}")
    public ResponseResult deleteStudentById(@PathVariable Integer id) {

        studentService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }



    @KgcLogs(model = "学生模块",operation = KgcOperation.ADD)
    @ApiOperation("批量导入")
    @PostMapping("batchImport")
    public ResponseResult batchImportStudent(MultipartFile file) {
        return studentService.batchImportStudent(file);
    }



    @ApiOperation("批量导出")
    @GetMapping("excel")
    public void batchExportStudents(String studentNo,String clazz, HttpServletResponse response) throws IOException {


        LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
        lambda.eq(StringUtils.isNotEmpty(studentNo),Student::getStudentNo,studentNo)
                .eq(StringUtils.isNotEmpty(clazz),Student::getClazz,clazz);

        List<Student> students = studentService.list(lambda);

        ExportParams exportParams = new ExportParams("学生信息表", "学生信息");

        Workbook Workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, students);
        ServletOutputStream outputStream = response.getOutputStream();
        Workbook.write(outputStream);
        Workbook.close();
        outputStream.close();

    }
}
